package org.example.openapi.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.example.domain.system.constant.TokenConstants;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * OpenAPI 配置
 */
@Configuration
@EnableAutoConfiguration
@ConditionalOnProperty(name = "springdoc.openapi.enabled", matchIfMissing = true)
public class OpenApiConfig {

    @Bean
    @ConditionalOnMissingBean
    public OpenApiProperties swaggerProperties() {
        return new OpenApiProperties();
    }

    @Bean
    public OpenAPI openApi(OpenApiProperties swaggerProperties) {
        return new OpenAPI()
                .info(new Info()
                        .title(swaggerProperties.getTitle())
                        .description(swaggerProperties.getDescription())
                        .contact(
                                new Contact()
                                        .name(swaggerProperties.getContact().getName())
                                        .email(swaggerProperties.getContact().getEmail())
                                        .url(swaggerProperties.getContact().getUrl())
                        )
                        .version(swaggerProperties.getVersion()))
                .addSecurityItem(new SecurityRequirement().addList(TokenConstants.AUTHENTICATION))
                .components(new Components().addSecuritySchemes(TokenConstants.AUTHENTICATION,
                        new SecurityScheme().name(TokenConstants.AUTHENTICATION).type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("")));

    }


}
